home *** CD-ROM | disk | FTP | other *** search
- #!/usr/bin/perl -w -Iblib/arch -Iblib/lib -I/usr/lib/perl5/5.6.0/i386-linux -I/usr/lib/perl5/5.6.0 -I.
- #
- # Copyright (C) 1999-2001 VMware, Inc.
- # All Rights Reserved
- #
- # $Id: vmx.pl,v 1.33 2001/08/07 20:17:52 lazlo Exp $
-
- # Return the specific VMware product
- sub vmware_product {
- return '@@PRODUCT@@';
- }
-
- BEGIN {
- if ( $^O eq "MSWin32" ) {
- my $PREFIX_PATH;
- if (-d '%winlibdir%') {
- $PREFIX_PATH = '%winlibdir%';
- } else {
- $PREFIX_PATH = 'C:\Progra~1\VMware\VMware~1\Programs';
- }
- @INC = ("$PREFIX_PATH/5.00503/lib",
- "$PREFIX_PATH/5.00503/lib/MSWin32-x86",
- "$PREFIX_PATH/site/5.00503/lib",
- "$PREFIX_PATH/site/5.00503/lib/MSWin32-x86",
- "$PREFIX_PATH/vmware/perlroot",
- '.'
- );
- }
-
- # Should be set to true only if you have a version of the Perl
- # interpreter derived from version 5.005 (ie. 5.00503 or 5.00504).
- if( vmware_product() eq 'server' ) {
- push(@INC, "/usr/lib/vmware/perl5/site_perl/5.005");
- push(@INC, "/usr/lib/vmware/perl5/site_perl/5.005/i386-linux");
- }
-
- # VMware internal development -- redirect Perl to find devel modules
- if (defined($ENV{TEST_TREE})) {
- if (-d $ENV{TEST_TREE} . "/apps/perl-control/blib/arch") {
- print STDERR "Using TEST_TREE for Perl API: $ENV{TEST_TREE}\n";
- unshift(@INC, $ENV{TEST_TREE} . "/apps/perl-control/blib/lib");
- unshift(@INC, $ENV{TEST_TREE} . "/apps/perl-control/blib/arch");
- } else {
- print STDERR "** Warning: TEST_TREE Perl API not found: $ENV{TEST_TREE}\n";
- }
- }
- }
-
- use strict;
- use 5.004;
-
- my $verbose = 0;
- my $quiet = 0;
-
- my $hostname = undef;
- my $port = 0;
- my $username = undef;
- my $password = undef;
-
- my $extension = ( $^O eq "MSWin32" ) ? ".vmx" : ".cfg";
- my $dirsep = ( $^O eq "MSWin32" ) ? "\\" : "/";
-
- sub usage {
- print STDERR "Usage: $0 <vmware-control-options> <vm-cfg-path> <vm-action> <arguments>\n";
- print STDERR " $0 -s <vmware-control-options> <server-action> <arguments>\n";
- print STDERR "\n";
- print STDERR " For example:\n";
- print STDERR " $0 -l -- lists VMs on the local server\n";
- print STDERR " $0 -s register foo$extension -- register a VM on the local server\n";
- # print STDERR " $0 -s enumerate -- lists VMs on the local server\n";
- # print STDERR " $0 -s exec <XML> -- send XML to vmware-serverd\n";
- print STDERR "\n $0 -v $dirsep" . "home$dirsep" . "vmware$dirsep" . "win2000$extension start\n";
- print STDERR " $0 -q $dirsep" . "home$dirsep" . "vmware$dirsep" . "win2000$extension get Status.power\n";
- exit(255);
- }
-
- sub pretty_print {
- while (defined($_ = shift)) {
- if (ref($_)) {
- print "[ ";
- if (ref($_) eq "ARRAY") {
- pretty_print(@{$_});
- } elsif (ref($_) eq "HASH") {
- pretty_print(%{$_});
- } else {
- pretty_print(${$_});
- }
- print "] ";
- } else {
- print $_, " ";
- }
- }
- }
-
- sub check_version {
- my $v = VMware::Control::version();
- if (! $v) {
- print STDERR "$0: Could not run Perl VMware::Control module\n";
- exit(255);
- }
-
- print "API Version: ",$v,"\n" if ($verbose);
- }
-
- sub get_server_local {
- check_version();
-
- # my $vms = &VMware::Control::Server::new();
- my $vms = &VMware::Control::Server::new($hostname, $port, $username, $password);
-
- return $vms;
- }
- sub connect_server_local {
-
- my $vms = get_server_local();
-
- if (! $vms || ! $vms->connect()) {
- my ($err, $errstr) = $vms->get_last_error();
- print STDERR "$0: Could not connect to vmware-authd\n";
- print STDERR " (VMControl error $err: $errstr)\n";
- exit(-$err);
- }
- return $vms;
- }
-
- sub do_list {
- my $vms = connect_server_local();
-
- my @list = $vms->enumerate();
-
- if (!defined($list[0])) {
- my ($err, $errstr) = $vms->get_last_error();
- print STDERR "VMControl error $err: $errstr\n";
- $vms->disconnect( );
- exit(-$err);
- }
-
- while($_ = shift @list) {
- print $_,"\n";
- }
- $vms->disconnect( );
- }
-
- sub do_server_action {
- my @args = @_;
-
- my $vms = get_server_local();
-
- if ($args[0] eq "register" || $args[0] eq "unregister") {
- if ( $^O eq "linux" && ! $args[1] =~ m%^/% ) {
- #Relative path --> absolute path (Linux)
- my $cwd;
- chomp($cwd = `pwd`);
- $args[1] = $cwd . "/" . $args[1];
- }
- }
-
- do_action($vms, @args);
- }
-
- sub do_vm_action {
- my $vmid = shift;
- my @args = @_;
-
- my $vms;
- if( vmware_product() eq 'server' ) {
- $vms = connect_server_local(); #XXX bad interface
- } else {
- $vms = get_server_local();
- }
-
- my $vm = &VMware::Control::VM::new($vms, $vmid);
-
- do_action($vm, @args);
- }
-
- sub do_action {
- my $obj = shift;
- my $action = shift;
- my @args = @_;
-
- if (! $obj || ! $obj->connect()) {
- my ($err, $errstr) = $obj->get_last_error();
- print STDERR "$0: Could not connect\n";
- print STDERR " (VMControl error $err: $errstr)\n";
- exit(-$err);
- }
-
- my @ret;
-
- eval {
- @ret = $obj->$action(@args);
- if (!defined($ret[0])) {
- my ($err, $errstr) = $obj->get_last_error();
- if ($err == VMware::Control::constant("VM_E_NOTCONNECTED", 0)) {
- $obj->connect(1);
- @ret = $obj->$action(@args);
- }
- }
- };
- if ($@) {
- #eval error
- print STDERR "Error executing the command \"$action\"\n\n";
- print STDERR $@ if ($verbose);
- usage();
- }
-
- if (!defined($ret[0])) {
- my ($err, $errstr) = $obj->get_last_error();
- print STDERR "VMControl error $err: $errstr\n";
- $obj->disconnect( );
- exit(-$err);
- }
-
- print "$action(@args) = " unless ($quiet);
- pretty_print(@ret);
- print "\n";
-
- $obj->disconnect( );
- }
-
- sub main {
-
- # Attempt to load the VMware::Control Perl libraries
- eval {
- require VMware::Control;
- require VMware::Control::Server;
- require VMware::Control::VM;
- };
- if ($@) {
- print STDERR
- "$0 requires the VMware::Control Perl libraries to be installed.\n";
- print STDERR "Check that your installation did not encounter errors.\n";
- exit(255);
- }
-
- my $i;
-
- # Scan the command line
- my @arguments = ();
- my $list = 0;
- my $server = 0;
-
- for ($i = 0; $i < $#ARGV + 1; $i++) {
- if ($ARGV[$i] eq "--") {
- last;
- }
- if ($ARGV[$i] eq "-v") {
- $verbose = 1;
- next;
- }
- if ($ARGV[$i] eq "-l") {
- $list = 1;
- next;
- }
- if ($ARGV[$i] eq "-s") {
- $server = 1;
- next;
- }
- if ($ARGV[$i] eq "-q") {
- $quiet = 1;
- next;
- }
- push @arguments, $ARGV[$i];
- }
-
- # Check the options validity
- if ($list) {
- if ($#arguments != -1) {
- usage();
- }
- do_list();
- } else {
- if ($server) {
- #Require at least 1 argument (server-action)
- if ($#arguments < 0) {
- usage();
- }
- do_server_action(@arguments);
- } else {
- #Require at least 2 arguments (config file, vm-action)
- if ($#arguments < 1) {
- usage();
- }
- do_vm_action(@arguments);
- }
- }
- return 0;
- }
-
- main();
-
- __END__
-
- =head1 NAME
-
- vmware-control -- Command-line control of VMware virtual machines.
-
- =head1 SYNOPSIS
-
- # List VMs registered on the local server
- vmware-control -l
-
- # Find out if a particular VM is powered on.
- vmware-control /home/vmware/win2000.cfg get Status.power
-
- # Same thing, but do not print the query (only print the result).
- vmware-control -q /home/vmware/win2000.cfg get Status.power
-
- # Same thing, on windows:
- vmware-control C:\home\vmware\win2000.vmx get Status.power
-
- # Power on a particular VM. Be verbose.
- vmware-control -v /home/vmware/win2000.cfg start
-
- # Same thing, on windows:
- vmware-control -v C:\home\vmware\win2000.vmx start
-
- =head1 DESCRIPTION
-
- This program provides a simple command-line interface to most of the
- operations in the VMware::Control Perl module.
-
- See the VMware Perl API documentation for more information.
-